using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using Microsoft.Expression.Interactivity.Input;

namespace Accountant.Utils
{
  public class MouseTrigger : EventTriggerBase<UIElement>
  {
    public static readonly DependencyProperty KeyProperty =
      DependencyProperty.Register("Key", typeof(Key), typeof(MouseTrigger));
    public static readonly DependencyProperty ModifiersProperty = DependencyProperty.
      Register("Modifiers", typeof(ModifierKeys), typeof(MouseTrigger));
    public static readonly DependencyProperty ActiveOnFocusProperty = DependencyProperty.
      Register("ActiveOnFocus", typeof(bool), typeof(MouseTrigger));
    public static readonly DependencyProperty FiredOnProperty = DependencyProperty.
      Register("FiredOn", typeof(KeyTriggerFiredOn), typeof(MouseTrigger));
    
//    private UIElement _targetElement;

    public Key Key
    {
      get { return (Key) GetValue(KeyProperty); }
      set { SetValue(KeyProperty, value); }
    }

    public ModifierKeys Modifiers
    {
      get { return (ModifierKeys) GetValue(ModifiersProperty); }
      set { SetValue(ModifiersProperty, value); }
    }

    public bool ActiveOnFocus
    {
      get { return (bool) GetValue(ActiveOnFocusProperty); }
      set { SetValue(ActiveOnFocusProperty, value); }
    }

    public KeyTriggerFiredOn FiredOn
    {
      get { return (KeyTriggerFiredOn) GetValue(FiredOnProperty); }
      set { SetValue(FiredOnProperty, value); }
    }

    protected override string GetEventName()
    {
      return "MouseDoubleClick";
    }

    private void OnKeyPress(object sender, KeyEventArgs e)
    {
      if (e.Key != Key || Keyboard.Modifiers != GetActualModifiers(e.Key, Modifiers))
        return;
      InvokeActions(e);
    }

    private static ModifierKeys GetActualModifiers(Key key, ModifierKeys modifiers)
    {
      if (key == Key.LeftCtrl || key == Key.RightCtrl)
        modifiers |= ModifierKeys.Control;
      else if (key == Key.LeftAlt || key == Key.RightAlt || key == Key.System)
        modifiers |= ModifierKeys.Alt;
      else if (key == Key.LeftShift || key == Key.RightShift)
        modifiers |= ModifierKeys.Shift;
      return modifiers;
    }

    protected override void OnEvent(EventArgs eventArgs)
    {
      InvokeActions(eventArgs);
//      _targetElement = !ActiveOnFocus ? GetRoot(Source) : Source;
//      if (FiredOn == KeyTriggerFiredOn.KeyDown)
//        _targetElement.KeyDown += OnKeyPress;
//      else
//        _targetElement.KeyUp += OnKeyPress;
    }

//    protected override void OnDetaching()
//    {
//      if (_targetElement != null)
//      {
//        if (FiredOn == KeyTriggerFiredOn.KeyDown)
//          _targetElement.KeyDown -= OnKeyPress;
//        else
//          _targetElement.KeyUp -= OnKeyPress;
//      }
//      base.OnDetaching();
//    }

    private static UIElement GetRoot(DependencyObject current)
    {
      var uiElement = default(UIElement);
      for (; current != null; current = VisualTreeHelper.GetParent(current))
        uiElement = current as UIElement;
      return uiElement;
    }
  }
}